package com.ruoyi.bill.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.bill.domain.DefaultAccountSet;
import com.ruoyi.bill.domain.Industry;
import com.ruoyi.bill.mapper.DefaultAccountSetMapper;
import com.ruoyi.bill.mapper.IndustryMapper;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.bill.mapper.AccountSetMapper;
import com.ruoyi.bill.domain.AccountSet;
import com.ruoyi.bill.service.IAccountSetService;

/**
 * 账套主Service业务层处理
 * 
 * @author 孙帆科
 * @date 2025-02-23
 */
@Service
public class AccountSetServiceImpl extends ServiceImpl<AccountSetMapper, AccountSet> implements IAccountSetService
{
    @Autowired
    private AccountSetMapper accountSetMapper;

    @Autowired
    private DefaultAccountSetMapper defaultAccountSetMapper;

    /**
     * 查询账套主
     * 
     * @param accountId 账套主主键
     * @return 账套主
     */
    @Override
    public AccountSet selectAccountSetByAccountId(Long accountId)
    {
        return accountSetMapper.selectAccountSetByAccountId(accountId);
    }

    /**
     * 查询账套主列表
     * 
     * @param accountSet 账套主
     * @return 账套主
     */
    @Override
    public List<AccountSet> selectAccountSetList(AccountSet accountSet)
    {
        return accountSetMapper.selectAccountSetList(accountSet);
    }

    /**
     * 新增账套主
     * 
     * @param accountSet 账套主
     * @return 结果
     */
    @Override
    public int insertAccountSet(AccountSet accountSet)
    {
//        accountSet.setCreateTime(DateUtils.getNowDate());
        return accountSetMapper.insert(accountSet);
    }

    /**
     * 修改账套主
     * 
     * @param accountSet 账套主
     * @return 结果
     */
    @Override
    public int updateAccountSet(AccountSet accountSet)
    {
//        return accountSetMapper.updateById(accountSet);
        return accountSetMapper.updateAccountSet(accountSet);
    }

    /**
     * 批量删除账套主
     * 
     * @param accountIds 需要删除的账套主主键
     * @return 结果
     */
    @Override
    public int deleteAccountSetByAccountIds(Long[] accountIds)
    {
        return accountSetMapper.deleteAccountSetByAccountIds(accountIds);
    }

    /**
     * 删除账套主信息
     * 
     * @param accountId 账套主主键
     * @return 结果
     */
    @Override
    public int deleteAccountSetByAccountId(Long accountId)
    {
        return accountSetMapper.deleteAccountSetByAccountId(accountId);
    }

    @Override
    public List<AccountSet> listAccountSetAllById() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        return accountSetMapper.listAccountSetAllById(loginUser.getSysUser().getUserId());
    }

    @Override
    public int checkDefaultAccoutSet(Long accountId) {
        //1.获取当前登录用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        //2.有的话，判断 当前账套 已经是默认账套了，不做操作
        LambdaQueryWrapper<DefaultAccountSet> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DefaultAccountSet::getUserId,loginUser.getSysUser().getUserId());
        List<DefaultAccountSet> defaultAccountSets = defaultAccountSetMapper.selectList(queryWrapper);

        if (defaultAccountSets.size()>0){
            DefaultAccountSet defaultAccountSet = defaultAccountSets.get(0);

            //是默认的账套，不做操作
            if (defaultAccountSet.getAccountSetId().equals(accountId)){
                throw new RuntimeException("已经是默认账套了");
            }
            //不是默认的账套，把原来的默认账套 删掉
            defaultAccountSetMapper.delete(queryWrapper);
        }
        //4.添加默认账套
        DefaultAccountSet defaultAccountSet = new DefaultAccountSet();
        defaultAccountSet.setUserId(loginUser.getSysUser().getUserId());
        defaultAccountSet.setAccountSetId(Long.valueOf(accountId));
        return defaultAccountSetMapper.insert(defaultAccountSet);
//        LoginUser loginUser = SecurityUtils.getLoginUser();
//        LambdaQueryWrapper<Object> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(DefaultAccountSet::getUserId,loginUser.getUserid());
//        return 0;
    }
}
